home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / DefaultDesktopManager.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  11.4 KB  |  352 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)DefaultDesktopManager.java    1.18 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. package javax.swing;
  17.  
  18. import java.awt.*;
  19. import java.beans.PropertyVetoException;
  20. import java.beans.PropertyChangeEvent;
  21. import javax.swing.border.Border;
  22. import java.awt.event.ComponentListener;
  23. import java.awt.event.ComponentAdapter;
  24. import java.awt.event.ComponentEvent;
  25.  
  26. /** This is an implementaion of the DesktopManager. It currently implements a
  27.   * the basic behaviors for managing JInternalFrames in an arbitrary parent.
  28.   * JInternalFrames that are not children of a JDesktop will use this component
  29.   * to handle their desktop-like actions.
  30.   * @see JDesktopPane
  31.   * @see JInternalFrame
  32.   * @version 1.18 08/26/98
  33.   * @author David Kloba
  34.   */
  35. public class DefaultDesktopManager implements DesktopManager, java.io.Serializable {
  36.     final static String PREVIOUS_BOUNDS_PROPERTY = "previousBounds";
  37.     final static String HAS_BEEN_ICONIFIED_PROPERTY = "wasIconOnce";
  38.  
  39.     final static int DEFAULT_DRAG_MODE = 0;
  40.     final static int OUTLINE_DRAG_MODE = 1;
  41.   
  42.     int dragMode = DEFAULT_DRAG_MODE;
  43.  
  44.     private static JInternalFrame currentActiveFrame = null;
  45.  
  46.     /** Normally this method will not be called. If it is, it
  47.       * try to determine the appropriate parent from the desktopIcon of the frame.
  48.       * Will remove the desktopIcon from it's parent if it successfully adds the frame.
  49.       */
  50.     public void openFrame(JInternalFrame f) {
  51.         if(f.getDesktopIcon().getParent() != null) {
  52.             f.getDesktopIcon().getParent().add(f);
  53.             removeIconFor(f);
  54.         }
  55.     }
  56.  
  57.     /** Removes the frame, and if necessary the desktopIcon, from it's parent. */
  58.     public void closeFrame(JInternalFrame f) {
  59.         if(f.getParent() != null) {
  60.             Container c = f.getParent();
  61.             c.remove(f);
  62.             c.repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight());
  63.         }
  64.         removeIconFor(f);
  65.         if(getPreviousBounds(f) != null)
  66.             setPreviousBounds(f, null);
  67.         if(wasIcon(f))
  68.             setWasIcon(f, null);
  69.     }
  70.  
  71.     /** Resizes the frame to fill it's parents bounds. */
  72.     public void maximizeFrame(JInternalFrame f) {
  73.  
  74.         Rectangle p;
  75.         if(!f.isIcon()) {
  76.         setPreviousBounds(f, f.getBounds());
  77.             p = f.getParent().getBounds();
  78.         } else {
  79.              Container c = f.getDesktopIcon().getParent();
  80.             if(c == null)
  81.                 return;
  82.             p = c.getBounds();
  83.             try { f.setIcon(false); } catch (PropertyVetoException e2) { }
  84.         }
  85.         setBoundsForFrame(f, 0, 0, p.width, p.height);
  86.         try { f.setSelected(true); } catch (PropertyVetoException e2) { }
  87.  
  88.         removeIconFor(f);
  89.     }
  90.  
  91.     /** Restores the frame back to it's size and position prior to a maximizeFrame()
  92.       * call.
  93.       */
  94.     public void minimizeFrame(JInternalFrame f) {
  95.         if(getPreviousBounds(f) != null) {
  96.             Rectangle r = getPreviousBounds(f);
  97.             setPreviousBounds(f, null);
  98.             try { f.setSelected(true); } catch (PropertyVetoException e2) { }
  99.             if(f.isIcon())
  100.                 try { f.setIcon(false); } catch (PropertyVetoException e2) { }
  101.             setBoundsForFrame(f, r.x, r.y, r.width, r.height);
  102.         }
  103.         removeIconFor(f);
  104.     }
  105.  
  106.     /** Removes the frame from it's parent and adds it's desktopIcon to the parent. */
  107.     public void iconifyFrame(JInternalFrame f) {
  108.         JInternalFrame.JDesktopIcon desktopIcon;
  109.         Container c;
  110.  
  111.         desktopIcon = f.getDesktopIcon();
  112.         if(!wasIcon(f)) {
  113.             Rectangle r = getBoundsForIconOf(f);
  114.             desktopIcon.setBounds(r.x, r.y, r.width, r.height);
  115.             setWasIcon(f, Boolean.TRUE);
  116.         }
  117.  
  118.         c = f.getParent();
  119.         c.remove(f);
  120.         c.add(desktopIcon);
  121.         c.repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight());
  122.         try { f.setSelected(false); } catch (PropertyVetoException e2) { }
  123.     }
  124.  
  125.     /** Removes the desktopIcon from it's parent and adds it's frame to the parent. */
  126.     public void deiconifyFrame(JInternalFrame f) {
  127.         JInternalFrame.JDesktopIcon desktopIcon;
  128.         Dimension size;
  129.  
  130.         desktopIcon = f.getDesktopIcon();
  131.         if(desktopIcon.getParent() != null) {
  132.             desktopIcon.getParent().add(f);
  133.             removeIconFor(f);
  134.             try { f.setSelected(true); } catch (PropertyVetoException e2) { }
  135.         }
  136.     }
  137.  
  138.     /** This will activate <b>f</b> moving it to the front. It will
  139.       * set the current active frame (if any) IS_SELECTED_PROPERTY to false.
  140.       * There can be only one active frame across all Layers.
  141.       */
  142.     public void activateFrame(JInternalFrame f) {
  143.         Container p = f.getParent();
  144.         Component[] c;
  145.  
  146.     // fix for bug: 4162443
  147.         if(p == null) {
  148.             // If the frame is not in parent, it's icon maybe, check it
  149.             p = f.getDesktopIcon().getParent();
  150.             if(p == null)
  151.                 return;
  152.         }
  153.     // we only need to keep track of the currentActive InternalFrame, if any
  154.     if (currentActiveFrame == null){
  155.       currentActiveFrame = f;
  156.     }
  157.     else if (currentActiveFrame != f) {  
  158.       // if not the same frame as the current active
  159.       // we deactivate the current 
  160.       if (currentActiveFrame.isSelected()) { 
  161.         try {
  162.           currentActiveFrame.setSelected(false);
  163.         }
  164.         catch(PropertyVetoException e2) {}
  165.       }
  166.       currentActiveFrame = f;
  167.     }
  168.     f.moveToFront();
  169.     }
  170.     
  171.     // implements javax.swing.DesktopManager
  172.     public void deactivateFrame(JInternalFrame f) {
  173.       if (currentActiveFrame == f)
  174.     currentActiveFrame = null;
  175.     }
  176.  
  177.     // implements javax.swing.DesktopManager
  178.     public void beginDraggingFrame(JComponent f) {
  179.         JDesktopPane p = getDesktopPane(f);
  180.     String mode = (String)p.getClientProperty("JDesktopPane.dragMode");
  181.     if (mode == null) {
  182.         dragMode = DEFAULT_DRAG_MODE;
  183.     } else if (mode.equals("outline")) {
  184.         dragMode = OUTLINE_DRAG_MODE;
  185.     }
  186.     }
  187.  
  188.     transient Point currentLoc = null;
  189.  
  190.     /** Calls setBoundsForFrame() with the new values. */
  191.     public void dragFrame(JComponent f, int newX, int newY) {
  192.         if ( dragMode == DEFAULT_DRAG_MODE ) {
  193.         setBoundsForFrame(f, newX, newY, f.getWidth(), f.getHeight());
  194.     } else {
  195.         JDesktopPane desktopPane = getDesktopPane(f);
  196.         Graphics g = desktopPane.getGraphics();
  197.  
  198.         g.setXORMode(Color.white);
  199.         if (currentLoc != null) {
  200.             g.drawRect( currentLoc.x, currentLoc.y, f.getWidth()-1, f.getHeight()-1);
  201.         }
  202.         g.drawRect( newX, newY, f.getWidth()-1, f.getHeight()-1);
  203.         currentLoc = new Point (newX, newY);
  204.         g.setPaintMode();
  205.     }
  206.     }
  207.  
  208.     // implements javax.swing.DesktopManager
  209.     public void endDraggingFrame(JComponent f) {
  210.         if ( dragMode == OUTLINE_DRAG_MODE && currentLoc != null) {
  211.         setBoundsForFrame(f, currentLoc.x, currentLoc.y, f.getWidth(), f.getHeight() );
  212.         currentLoc = null;
  213.     }
  214.     }
  215.  
  216.     // implements javax.swing.DesktopManager
  217.     public void beginResizingFrame(JComponent f, int direction) {}
  218.  
  219.     /** Calls setBoundsForFrame() with the new values. */
  220.     public void resizeFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
  221.         setBoundsForFrame(f, newX, newY, newWidth, newHeight);
  222.     }
  223.  
  224.     // implements javax.swing.DesktopManager
  225.     public void endResizingFrame(JComponent f) {}
  226.  
  227.     // PENDING(klobad) this should be optimized
  228.     /** This moves the JComponent and repaints the damaged areas. */
  229.     public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
  230.         boolean didResize = (f.getWidth() != newWidth || f.getHeight() != newHeight);
  231.         f.setBounds(newX, newY, newWidth, newHeight);
  232.         if(didResize) {
  233.             f.validate();
  234.         }
  235.     }
  236.  
  237.     /** Convience method to remove the desktopIcon of <b>f</b> is necessary. */
  238.     protected void removeIconFor(JInternalFrame f) {
  239.         JInternalFrame.JDesktopIcon di = f.getDesktopIcon();
  240.         Container c = di.getParent();
  241.         if(c != null) {
  242.             c.remove(di);
  243.             c.repaint(di.getX(), di.getY(), di.getWidth(), di.getHeight());
  244.         }
  245.     }
  246.  
  247.     /** The iconifyFrame() code calls this to determine the proper bounds
  248.       * for the desktopIcon.
  249.       */
  250.     protected Rectangle getBoundsForIconOf(JInternalFrame f) {
  251.         Rectangle p = f.getParent().getBounds();
  252.         Dimension prefSize = f.getDesktopIcon().getPreferredSize();
  253.         int x2, y2, w2, h2;
  254.  
  255.         w2 = prefSize.width;
  256.         h2 = prefSize.height;
  257.         x2 = 0;
  258.         y2 = p.height - h2;
  259.  
  260.         if(f.getParent() == null && f.getDesktopIcon().getParent() == null) {
  261.             return new Rectangle(x2, y2, w2, h2);
  262.         }
  263.  
  264.         Container lp = (Container)f.getParent();
  265.         if(lp == null)
  266.             lp = f.getDesktopIcon().getParent();
  267.  
  268.         int pos = lp.getComponentCount();
  269.         Component c[] = lp.getComponents();
  270.         Rectangle b;
  271.         int i, x3 = 0, y3 = p.height;
  272.         JInternalFrame next;
  273.         for(i = pos - 1; i >=0; i--)    {
  274.             next = null;
  275.             if(lp.getComponent(i) instanceof JInternalFrame)
  276.                 next = (JInternalFrame)lp.getComponent(i);
  277.             else if(lp.getComponent(i) instanceof JInternalFrame.JDesktopIcon)
  278.                 next = ((JInternalFrame.JDesktopIcon)lp.getComponent(i)).getInternalFrame();
  279.             if(next != null) {
  280.                 if(next != f && wasIcon(next)) {
  281.                     b = next.getDesktopIcon().getBounds();
  282.                     if(b.y < y3) {
  283.                         y3 = b.y;
  284.                         x3 = b.x + b.width;
  285.                     } else if(b.y == y3) {
  286.                         if(b.x + b.width > x3)
  287.                             x3 = b.x + b.width;
  288.                     }
  289.                 }
  290.             }
  291.         }
  292.  
  293.         if(y3 != p.height)
  294.             y2 = y3;
  295.         if(x3 + w2 > p.width) {
  296.             y2 -= h2;
  297.             x2 = 0;
  298.         } else {
  299.             x2 = x3;
  300.         }
  301.  
  302.         return new Rectangle(x2, y2, w2, h2);
  303.     }
  304.  
  305.     /** Stores the bounds of the component just before a maximize call. */
  306.     protected void setPreviousBounds(JInternalFrame f, Rectangle r)     {
  307.     if (r != null) {
  308.         f.putClientProperty(PREVIOUS_BOUNDS_PROPERTY, r);
  309.     }
  310.     }
  311.  
  312.     protected Rectangle getPreviousBounds(JInternalFrame f)     {
  313.         return (Rectangle)f.getClientProperty(PREVIOUS_BOUNDS_PROPERTY);
  314.     }
  315.  
  316.     /** Sets that the component has been iconized and the bounds of the
  317.       * desktopIcon are valid.
  318.       */
  319.     protected void setWasIcon(JInternalFrame f, Boolean value)  {
  320.     if (value != null) {
  321.         f.putClientProperty(HAS_BEEN_ICONIFIED_PROPERTY, value);
  322.     }
  323.     }
  324.  
  325.     protected boolean wasIcon(JInternalFrame f) {
  326.         return (f.getClientProperty(HAS_BEEN_ICONIFIED_PROPERTY) == Boolean.TRUE);
  327.     }
  328.  
  329.  
  330.     JDesktopPane getDesktopPane( JComponent frame ) {
  331.         JDesktopPane pane = null;
  332.     Component c = frame.getParent();
  333.  
  334.         // Find the JDesktopPane
  335.         while ( pane == null ) {
  336.         if ( c instanceof JDesktopPane ) {
  337.             pane = (JDesktopPane)c;
  338.         }
  339.         else if ( c == null ) {
  340.             break;
  341.         }
  342.         else {
  343.             c = c.getParent();
  344.         }
  345.     }
  346.  
  347.     return pane;
  348.     }
  349.  
  350. }
  351.  
  352.